package com.demo.lock.jdk;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaphoreLock implements Runnable {
    final Semaphore semp = new Semaphore(10, true);


    @Override
    public void run() {
        try {
            semp.acquire();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+"继续执行任务");
            semp.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        ExecutorService exec = Executors.newFixedThreadPool(10);
        SemaphoreLock lock = new SemaphoreLock();
        for (int i = 0; i < 70; i++) {
            exec.submit(lock);
        }
    }
}
